package com.ruoyi.business;

import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.comm.Protocol;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.Bucket;
import com.aliyun.oss.model.PutObjectRequest;

import java.io.ByteArrayInputStream;
import java.util.List;

public class Test {

    public static void main(String[] args) throws Exception {

        CredentialsProvider credentialsProvider = new DefaultCredentialProvider("LTAI5tNGqy7to4tjKTBXdeMX", "5GlSRSp2aMV3r5rTJ2IfNrS8ZRFYgF");

        // 使用credentialsProvider初始化客户端
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        // 显式声明使用 V4 签名算法
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        // 创建OSSClient实例。
        // 当OSSClient实例不再使用时，调用shutdown方法以释放资源。
        OSS ossClient = OSSClientBuilder.create()
                .endpoint("oss-cn-beijing.aliyuncs.com")
                .credentialsProvider(credentialsProvider)
                .clientConfiguration(clientBuilderConfiguration)
                .region("cn-beijing")
                .build();

        try {


            // 填写目录名称，目录需以正斜线结尾。
            String objectName = "exampledir/dir01/dir02/";
            String content = "";

            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest("segbucket", objectName, new ByteArrayInputStream(content.getBytes()));

            // 上传字符串。
            ossClient.putObject(putObjectRequest);


        } finally {
            // 当OSSClient实例不再使用时，调用shutdown方法以释放资源
            ossClient.shutdown();
        }
    }
}
